package com.ycy.analyzer.ansj;

import org.junit.Test;
import org.nlpcn.commons.lang.tire.GetWord;
import org.nlpcn.commons.lang.tire.domain.Forest;
import org.nlpcn.commons.lang.tire.library.Library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author hankcs
 */
public class Main {

  public static void main(String[] args) throws IOException {

//    BufferedReader reader = new BufferedReader(new FileReader("./data/small.dic"));

    String line;

    Set<Character> charset = new HashSet<Character>();

    List<String> words = new ArrayList<String>();
    words.add("一举");
    words.add("一举一动");
    words.add("一举成名");
    words.add("一举成名天下知");
    words.add("万能");
    words.add("万能胶");

    for (String word : words) {
      char[] chars = word.toCharArray();
      for (char c : chars) {
        charset.add(c);
      }
    }

    String infoCharsetValue = "";
    String infoCharsetCode = "";
    for (Character c : charset) {
      infoCharsetValue += c.charValue() + "    ";
      infoCharsetCode += (int) c.charValue() + " ";
    }
    infoCharsetValue += '\n';
    infoCharsetCode += '\n';
    System.out.print(infoCharsetValue);
    System.out.print(infoCharsetCode);


//    while ((line = reader.readLine()) != null) {
//      words.add(line);
//      // 制作一份码表debug
//      for (char c : line.toCharArray()) {
//        charset.add(c);
//      }
//    }
//    reader.close();

    // 这个字典如果要加入新词必须按字典序，参考下面的代码
//        Collections.sort(words);
//        BufferedWriter writer = new BufferedWriter(new FileWriter("./data/sorted.dic", false));
//        for (String w : words)
//        {
//            writer.write(w);
//            writer.newLine();
//        }
    System.out.println("字典词条：" + words.size());


    DoubleArrayTrie dat = new DoubleArrayTrie();
    System.out.println("是否错误: " + dat.build(words));
    System.out.println(dat);
    List<Integer> integerList = dat.commonPrefixSearch("一举成名天下知");
    for (int index : integerList) {
      System.out.println(words.get(index));
    }

  }


  /**
   * 测试 ansj 的forest
   * @throws Exception
   */
  @Test
  public void test1() throws Exception {
    /*
     * 词典的构造.一行一个词后面是参数.可以从文件读取.可以是read流.
     */
    String dic =
            "中国\t1\tzg\n" +
                    "人名\t2\n" +
                    "中国人民\t4\n" +
                    "人民\t3\n" +
                    "孙健\t5\n" +
                    "CSDN\t6\n" +
                    "java\t7\n" +
                    "java学习\t10\n";
    Forest forest = Library.makeForest(new BufferedReader(new StringReader(dic)));

//    /*
//     * 删除一个单词
//     */
//    Library.removeWord(forest, "中国");

//    /*
//     * 增加一个新词
//     */
//    Library.insertWord(forest, "中国人");

    String content = "中国人名识别是中国人民的一个骄傲.孙健人民在CSDN中学到了很多最早iteye是java学习笔记叫javaeye但是java123只是一部分";
    GetWord udg = forest.getWord(content);

    String temp = null;
    while ((temp = udg.getFrontWords()) != null)
      System.out.println(temp + "\t\t" + udg.getParam(1) + "\t\t" + udg.getParam(2));

  }

}
